/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.menu;

import apus.command.AbstractCommand;
import apus.command.ExitCommand;
import apus.command.UpCommand;
import apus.exception.BusinessException;
import apus.exception.ErrorOutput;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * The <code>Menu</code> class represents menu of console project. Menu extends
 * AbstractCommand to realize submenu.
 * @author Maxim Vasilevsky
 * @author Roman Dyatkovsky
 * @since APUS v0.4
 * @see AbstractCommand
 */
public class Menu extends AbstractCommand {

    private Map<Integer, AbstractCommand> commands = new HashMap<>();
    private int counter = 0;
    private static Scanner scanner = new Scanner(System.in, "Cp1251");
    static protected Menu currentMenu;
    static protected Menu mainMenu;
    protected Menu menuSt;

    public Menu(String menuName) {
        super(menuName);
    }

    @Override
    public void execute() {
        
       //6
        scanner.useDelimiter("\n");
                
        if (!getMainMenu().equals(this)) {
            
            System.out.println("------ " + this.getName() + " menu ------");

            Menu tempmenu = new UpCommand("Back");
            tempmenu.setMenuSt(menuSt);
            commands.put(counter + 1, tempmenu);
            commands.put(counter+2, new ExitCommand());

           /* tempmenu = new UpCommand("To main menu");
            tempmenu.setMenuSt(getMainMenu());
            commands.put(counter + 2, tempmenu);*/
            Menu.setCurrentMenu(this);
            
        } else {
            menuSt = this;
            System.out.println("------ APUS Information system ------");
        }
        for (Map.Entry<Integer, AbstractCommand> entry : commands.entrySet()) {
            if (!entry.getValue().getClass().equals(ExitCommand.class)) {
                System.out.println(entry.getKey() + " " + entry.getValue().getName()+ " ...");
            } else {
                System.out.println(entry.getKey() + " " + entry.getValue().getName());
            }
        }
        System.out.println("  Enter item number:");
        int item=counter+2;
        try {
        item = scanner.nextInt();
        } catch (Exception e) {
            scanner.next();
            throw new BusinessException("Incorrect number format", e);
        }
        AbstractCommand command = commands.get(item);
         if (command == null) {
            throw new BusinessException("Wrong item number");
        }
        if (!command.getParamNames().isEmpty()) {
            //System.out.println("Enter params value");
            for (String paramName : command.getParamNames()) {
                System.out.println("Enter " + paramName);
                String paramValue = scanner.next();
                command.setParameter(paramName, paramValue);
            }
        }
        try {
            command.execute();
        } catch (BusinessException e) {
            System.out.println("Exception while command execution: " + e.getMessage());
            ErrorOutput.WriteError("" + new Date() + "  " + e.getMessage());
        }
    }

    /**
     * Adding command to the menu.
     *
     * @param command Command to add
     */
    public void addCommand(AbstractCommand command) {
        commands.put(++counter, command);
        if (command.getClass().equals(Menu.class)) {
            ((Menu) command).setMenuSt(this);
        }
    }

    public void setMenuSt(Menu menuSt) {
        this.menuSt = menuSt;
    }

    public static void setCurrentMenu(Menu mainMenu) {
        Menu.currentMenu = mainMenu;
    }

    public static Menu getCurrentMenu() {
        return currentMenu;
    }

    public static Menu getMainMenu() {
        return mainMenu;
    }

    public static void setMainMenu(Menu mainMenu) {
        Menu.mainMenu = mainMenu;
    }
}
